package com.kevin.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * 文件工具类
 * 
 * @author Kevin
 */
public class FileUtils {
	static String lineSeparator = System.getProperty("line.separator"); 

	/**
	 * 创建多级目录
	 * 
	 * @param dir
	 * @return
	 */
	public static boolean mkdirs(String dirs) {
		new File(dirs).mkdirs();
		return true;
	}

	/**
	 * 创建文件夹和文件
	 * 
	 * @param path
	 * @return
	 */
	public static boolean touch(String path) {
		File file = new File(path);
		if (!file.exists()) {
			try {
				File parentFile = file.getParentFile();
				if (!parentFile.exists()) {
					parentFile.mkdirs();
				}
				file.createNewFile();
			} catch (IOException e) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 删除文件
	 * 
	 * @param path
	 * @return
	 */
	public static boolean delete(String path) {
		File file = new File(path);
		if (file.exists()) {
			return file.delete();
		}
		return false;
	}

	/**
	 * 文件复制
	 * 
	 * @param src
	 * @param dist
	 */
	public static void copy(String src, String dist) {
		copy(new File(src), new File(dist));
	}

	/**
	 * 文件复制（通过文件通道）
	 * 
	 * @param src
	 * @param dist
	 */
	public static void copy(File src, File target) {
		FileInputStream fi = null;
		FileOutputStream fo = null;
		FileChannel in = null;
		FileChannel out = null;
		try {
			fi = new FileInputStream(src);
			fo = new FileOutputStream(target);
			in = fi.getChannel();
			out = fo.getChannel();
			in.transferTo(0, in.size(), out);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fi != null)
					fi.close();
				if (in != null)
					in.close();
				if (fo != null)
					fo.close();
				if (out != null)
					out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 复制文件夹
	 * 
	 * @param sourceFolder
	 * @param targetFolder
	 * @throws IOException
	 */
	public static void copyFolder(String sourceFolder, String targetFolder)
			throws IOException {
		(new File(targetFolder)).mkdirs();
		File[] file = (new File(sourceFolder)).listFiles();
		for (int i = 0; i < file.length; i++) {
			if (file[i].isFile()) {
				File sourceFile = file[i];
				File targetFile = new File(
						new File(targetFolder).getAbsolutePath()
								+ File.separator + file[i].getName());
				copy(sourceFile, targetFile);
			}
			if (file[i].isDirectory()) {
				String dir1 = sourceFolder + File.separator + file[i].getName();
				String dir2 = targetFolder + File.separator + file[i].getName();
				copyFolder(dir1, dir2);
			}
		}
	}

	/**
	 * 剪切文件
	 * 
	 * @param s
	 * @param target
	 * @return
	 */
	public static boolean cut(String s, String target) {
		return new File(s).renameTo(new File(target));
	}

	/**
	 * 剪切文件覆盖
	 * 
	 * @param s
	 * @param target
	 * @return
	 */
	public static boolean replace(String src, String target) {
		File s = new File(src);
		File d = new File(target);
		if (d.exists()) {
			d.delete();
		}
		return s.renameTo(d);
	}

	/**
	 * 替换文件内容（位置不变）
	 * 
	 * @param path
	 * @param var
	 * @param con
	 */
	public static void replaceContent(String path, String var, String con) {
		replaceContent(path, path, var, con);
	}

	/**
	 * 替换文件内容
	 * 
	 * @param srcPath
	 * @param var
	 * @param con
	 */
	public static void replaceContent(String srcPath, String targetPath,
			String var, String con) {
		FileOutputStream fs = null;
		try {
			FileReader read = new FileReader(srcPath);
			BufferedReader br = new BufferedReader(read);
			StringBuilder content = new StringBuilder();
			while (br.ready() != false) {
				content.append(br.readLine().replace(var, con));
//				content.append(lineSeparator);
			}
			if (br != null)
				br.close();
			if (read != null)
				read.close();
			File file = new File(targetPath);
			File parentFile = file.getParentFile();
			if (!parentFile.exists())
				parentFile.mkdirs();
			fs = new FileOutputStream(targetPath);
			fs.write(content.toString().getBytes("utf-8"));
			fs.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fs != null)
				try {
					fs.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}
	/**
	 * 替换文件内容
	 * 
	 * @param srcPath
	 * @param var
	 * @param con
	 */
	public static void replaceContent(String srcPath, String targetPath,
			Map<String,String> map) {
		FileOutputStream fs = null;
		try {
			FileReader read = new FileReader(srcPath);
			BufferedReader br = new BufferedReader(read);
			StringBuilder content = new StringBuilder();
			while (br.ready() != false) {
				String readLine = br.readLine();
				readLine= replace(readLine,map);
				content.append(readLine);
				content.append(lineSeparator);
			}
			if (br != null)
				br.close();
			if (read != null)
				read.close();
			File file = new File(targetPath);
			File parentFile = file.getParentFile();
			if (!parentFile.exists())
				parentFile.mkdirs();
			fs = new FileOutputStream(targetPath);
			fs.write(content.toString().getBytes("utf-8"));
			fs.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fs != null)
				try {
					fs.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}

	/**
	 * 获取指定路径下所有文件名列表
	 * @param filePathList 要输出的filePathList
	 * @param strPath
	 */
	public static void getFilePathList(List<String> filePathList, String strPath) {
		File dir = new File(strPath);
		File[] files = dir.listFiles();
		if (files == null)
			return;
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				getFilePathList(filePathList, files[i].getAbsolutePath());
			} else {
				filePathList.add(files[i].getAbsolutePath());
			}
		}
	}
//	
//	/**
//	 * 获取指定路径下所有文件名列表
//	 * @param filePathList 要输出的filePathList
//	 * @param strPath
//	 */
//	public static void getFilePathList(List<String> filePathList, String strPath) {
//		File dir = new File(strPath);
//		File[] files = dir.listFiles();
//		if (files == null)
//			return;
//		for (int i = 0; i < files.length; i++) {
//			if (files[i].isDirectory()) {
//				getFilePathList(filePathList, files[i].getAbsolutePath());
//			} else {
//				filePathList.add(files[i].getAbsolutePath());
//			}
//		}
//	}
	
	
	public static String replace(String src,Map<String,String> key_value){
		Set<Entry<String, String>> entrySet = key_value.entrySet();
		for(Entry<String,String> entry:entrySet){
			String key = entry.getKey();
			String value = entry.getValue();
			src=src.replace(key, value);
		}
		return src;
	}

}
